﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace MyMathHelp
{
    public class MultiSegmentFunction
    {
        public class Segment
        {
            public Vector2 StartPoint;  //Y为温度  x为能量
            public Vector2 EndPoint;
        }
        //外层输入保证 不会垂直于X轴 不考虑 sab 为0的情况
        static bool DoFunction(Segment sa, Segment sb,ref Vector2 result)
        {
            //sa 的直线方程为
            float saa = sa.EndPoint.y - sa.StartPoint.y;
            float sab = sa.StartPoint.x - sa.EndPoint.x;
            float sac = sa.EndPoint.x * sa.StartPoint.y - sa.StartPoint.x * sa.EndPoint.y;

            //sb 的直线方程为
            float sba = sb.EndPoint.y - sb.StartPoint.y;
            float sbb = sb.StartPoint.x - sb.EndPoint.x;
            float sbc = sb.EndPoint.x * sb.StartPoint.y - sb.StartPoint.x * sb.EndPoint.y;
            if (Math.Abs( saa * sbb - sba * sab) < float.Epsilon)  //如果两直线平行   
            {
                //先判断是不是同一条直线
                //再判断是否相交
                if (Math.Abs(sab * sbc - sbb * sac) < float.Epsilon)    //如果为同一条直线的话
                {
                    if (sa.EndPoint.x > sb.StartPoint.x && sa.StartPoint.x < sb.StartPoint.x)
                    {
                        result = sb.StartPoint;
                        return true;
                    }
                    else if(sb.EndPoint.x > sa.StartPoint.x && sb.StartPoint.x < sa.StartPoint.x)
                    {
                        result = sa.StartPoint;
                        return true;
                    }
                }

                //平行的话也有可能相交 即重复
                return false;
            }

            float x = (sab * sbc - sbb * sac) / (saa * sbb - sba * sab);

            if ( x > sa.StartPoint.x * 0.9999 && x * 0.9999f<= sa.EndPoint.x && x > sb.StartPoint.x *0.9999f && x* 0.9999f <= sb.EndPoint.x)
            {
                float y = (sba * sac - saa * sbc) / (saa * sbb - sba * sab);
                result = new Vector2(x, y);
                return true;
            }
            return false;
        }

        public static List<Segment> Build(List<Vector2> points)
        {
            List<Segment> ss = new List<Segment>();
            for(int i = 0; i < points.Count - 1;i++)
            {
                Segment s = new Segment();
                s.StartPoint = points[i];
                s.EndPoint = points[i + 1];
                ss.Add(s);
            }
            return ss;
        }

        public static Vector2 DoFunction(List<Segment> LineA,List<Segment> LineB)
        {
            Vector2 result = Vector2.zero;
            for(int i = 0; i < LineA.Count;i++)
            {
                for(int j = 0;j < LineB.Count;j++)
                {
                    if(DoFunction(LineA[i], LineB[j],ref result))
                    {
                        return result;
                    }
                }
            }
            return result;
        }


    }
}
